<template>
  <div>
    <div style="display: flex;justify-content: flex-end;align-items: center;width: 100%;height: 100vh" class="bgImg">
      <div style="width: 400px; height: 385px;margin-right: 100px;
      display: flex;flex-wrap: wrap;justify-content: center;align-items: flex-start">
        <div style="width: 400px; height: 420px;background: #ffffff;
        border-radius: 10px;box-shadow: 0 2px 4px #989898;z-index: 5">
          <el-form  ref="registerForm" :model="user" :rules="rules" style="width: 260px; margin: 0 auto; padding-top: 20px">
            <div style="font-size: 30px;color: #30c2fe;margin: 10px auto;font-weight: bolder;
          display: flex;justify-content: center;align-items: center">
              <span>注 册</span>
            </div>
            <el-form-item prop="username">
              <el-input class="inputItem" v-model.trim="user.username" placeholder="请输入用户名"/>
            </el-form-item>
            <!--          <el-form-item prop="phone">-->
            <!--            <el-input class="inputItem" v-model.trim="user.phone" placeholder="请输入手机号"/>-->
            <!--          </el-form-item>-->
            <el-form-item prop="password">
              <el-input class="inputItem" v-model.trim="user.password" placeholder="请输入密码" show-password/>
            </el-form-item>
            <el-form-item prop="checkPwd">
              <el-input class="inputItem" v-model.trim="user.checkPwd" placeholder="请再次输入密码" show-password/>
            </el-form-item>
            <el-form-item prop="code">
              <div style="display: flex;align-items: center">
                <el-input class="inputItem" v-model.trim="user.code" placeholder="请输入验证码" @keyup.enter.native="handleRegister"/>
                <span style="cursor: pointer;" @click="refreshCode">
              <Identify :identifyCode="identifyCode"></Identify>
              </span>
              </div>
            </el-form-item>
            <el-button type="primary" style="width: 100%;background: #24c5ff" @click="handleRegister">注册</el-button>
            <el-form-item prop="agree" style="display:flex;justify-content: center;height: 30px">
              <el-checkbox v-model="user.agree" >我已阅读并同意<el-link type="primary" @click.stop="$router.push({name:'Protocol'})">《用户协议》</el-link></el-checkbox>
            </el-form-item>
          </el-form>
        </div>
        <div style="display:flex;justify-content: center;align-items:center;height: 35px;
            box-shadow: 0 1px 2px #7c7c7c;background: rgba(253,254,255,0.81);border-radius: 0 0 10px 10px;width: 350px; ">
          <span style="margin: 0 10px;color: #00c4ff;cursor: pointer" @click="$router.push('/login')">已有账号？去登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Identify from "@/components/common/Identify.vue";
import {register} from "@/api/user";
import {
  validateCaptcha,
  validateCheckPwd,
  validatePassword,
  validatePhone,
  validateUserNameAsync
} from "@/utils/validator";
import router from "@/router";

export default {
  name: "Register",
  computed: {
    rules() {
      return {
        username: {required: true, trigger: 'blur',
          validator: validateUserNameAsync},
        password: {required: true, trigger: 'blur',
          validator: validatePassword},
        //需要将用户两次输入的密码进行比较，所以需要在此处再包一层箭头函数，手动调用扩展参数列表的validator
        checkPwd: {required: true, trigger: 'blur',
          validator: (rule,value,callback)=>validateCheckPwd(value,this.user.password,callback)},
        // phone: {required: true, trigger: 'blur',
        //   validator: validatePhone},
        // 图片验证码校验器,manual表示只有手动调用检验函数时才触发
        code: {required: true, trigger:'manual',
          validator: (rule,value,callback)=>validateCaptcha(value,this.identifyCode,callback,this.refreshCode)},
        agree: {required: true, trigger: 'change',
          validator: (rule,value,callback)=> !value?callback(new Error('请阅读并勾选用户协议')):callback()}
      }
    }
  },
  components: {Identify},
  data() {
    return {
      agreed: false,
      user: {
        code: '',
        agree:false,
        username: '',
        password: '',
        checkPwd: '',
      },
      // 图片验证码
      identifyCode: '',
      // 验证码规则
      identifyCodeRule: '123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm',
    }
  },
  mounted() {
    this.refreshCode()
  },
  methods: {
    router() {
      return router
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid=>{
        if(valid){
          register(this.user).then(res => {
            if (res.code === '0') {
              this.$message({
                type: "success",
                message: "注册成功"
              })
              this.$router.replace("/login")
            } else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
          })
        }
      })
    },
    // 切换验证码
    refreshCode() {
      this.user.code = ''
      this.makeCode(4)
    },
    // 生成随机验证码
    makeCode(length) {
      this.identifyCode = ''
      for (let i = 0; i < length; i++) {
        this.identifyCode += this.identifyCodeRule[Math.floor(Math.random() * (this.identifyCodeRule.length))]
      }
    },
  }
}
</script>

<style scoped>
.bgImg {
  background: url("../assets/bg.webp") no-repeat;
  background-size: 100% 100vh;
}

::v-deep(.el-input__inner){
  color: #000000;
  box-shadow: inset #d9d8d8 0 0 5px;
}
::v-deep(.el-form-item__content){
  display: flex;
  justify-content: center;
}
</style>
